/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.manager.model;

import com.tamnhat.entity.Partner;
import com.tamnhat.entity.Product;
import com.tamnhat.entity.ProductPartner;
import com.telsoft.applet.util.DateUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class ProductModel {
    public static ArrayList<Product> getAll() throws Exception{
        ArrayList<Product> list = new ArrayList<Product>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = " select product_id,product_name,register_keyword from sdp_products ";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                Product p = new Product();
                p.setId(rs.getString(1));
                p.setName(rs.getString(2));
                p.setRegKeyword(rs.getString(3));
                p.setStatus(checkStatusProduct(p.getId(),cn,st));
                list.add(p);
            }
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return list;
    }
    public static ArrayList<ProductPartner> getProductPartner(String partnerId) throws Exception{
        ArrayList<ProductPartner> list = new ArrayList<ProductPartner>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = " select a.id,b.product_name,to_char(a.start_time,'dd-mm-yyyy'),to_char(a.end_time,'dd-mm-yyyy')"
                + " from sdp_product_partner a,sdp_products b "
                + " where partner_id = "+partnerId+" and a.product_id = b.product_id "
                + " order by a.id";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                ProductPartner p = new ProductPartner();
                p.setId(rs.getString(1));
                p.setProductName(rs.getString(2));
                p.setStartDate(rs.getString(3));
                p.setEndDate(rs.getString(4));
                list.add(p);
            }
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return list;
    }
    
    public static boolean checkStatusProduct(String id,Connection cn, Statement st) throws Exception{        
        ResultSet rs = null;
        String strSQL = " select to_char(end_time,'dd-mm-yyyy') from sdp_product_partner where product_id = "+id+" ";
        SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");
        try {
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {                
                String endStr = rs.getString(1);    
                if(endStr == null || endStr.equalsIgnoreCase("")){
                    return false;
                }
                Date eDate = DateUtil.toDate(endStr,"dd-MM-yyyy");
                Date nDate = new Date();
                if(eDate.compareTo(nDate) >= 0){
                    return false;
                }
            }
            return true;
        } finally {
            Database.closeObject(rs);            
        }
    }
        
}
